import { Context, Next } from 'koa';

import { RESPONSE_MAP } from '../service/response.ts';
import { ResponseCode } from '../types';

export const responseHandler = async (ctx: Context, next: Next) => {
  if (ctx.msg || ctx.code) {
    const code = (ctx.code as ResponseCode) || 200;
    ctx.body = {
      code: code,
      message: ctx.msg || RESPONSE_MAP[code],
      success: ctx.code === 200,
      result: ctx.body || null,
    };
  }
  return;
  // 通常不存在的 url 会走到此处，返回默认的404
};
